package com.isdust.jy.mbank.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.isdust.jy.mbank.MainActivity;
import com.isdust.jy.mbank.R;
import com.isdust.jy.mbank.base.BaseActivity;
import com.isdust.jy.mbank.util.DialogHelp;
import com.isdust.zor.core.action.ServerAction;
import com.isdust.zor.core.listener.onTaskCallBackListener;


public class LoginActivity extends BaseActivity {


    private AutoCompleteTextView mPhoneView;
    private EditText mPasswordView;
    private View mProgressView;
    private View mLoginFormView;
    private Dialog progress;
    private TextView login_reb;
    private TextView login_forget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mPhoneView = (AutoCompleteTextView) findViewById(R.id.Phone);
        mPasswordView = (EditText) findViewById(R.id.password);
        progress = DialogHelp.getWaitDialog(this, "登陆中，请稍后");

        Button mPhoneSignInButton = (Button) findViewById(R.id.phone_sign_in_button);
        mPhoneSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptLogin();
            }
        });

        mLoginFormView = findViewById(R.id.login_form);
        mProgressView = findViewById(R.id.login_progress);

        //跳转注册界面
        init1();
        //跳转更改密码界面
        init2();
    }



    private void init1(){
        login_reb = (TextView) findViewById(R.id.login_reb);
        login_reb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this , Index.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void init2(){
        login_forget = (TextView) findViewById(R.id.login_forget);
        login_forget.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this , ChangePass.class);
                startActivity(intent);
            }
        });
    }

    private void attemptLogin() {


        // Reset errors.
        mPhoneView.setError(null);
        mPasswordView.setError(null);

        // Store values at the time of the login attempt.
        String Phone = mPhoneView.getText().toString();
        String password = mPasswordView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password, if the user entered one.
        if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_invalid_password));
            focusView = mPasswordView;
            cancel = true;
        }

        // Check for a valid Phone address.
        if (TextUtils.isEmpty(Phone)) {
            mPhoneView.setError(getString(R.string.error_field_required));
            focusView = mPhoneView;
            cancel = true;
        } else if (!isPhoneValid(Phone)) {
            mPhoneView.setError(getString(R.string.error_invalid_phone));
            focusView = mPhoneView;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {
            progress.show();
//            ServerAction.login("admin", "admin", new onTaskCallBackListener<Void>() {
//                @Override
//                public void onSuccess() {
//                    progress.dismiss();
//                }
//
//                @Override
//                public void onFailure(String error) {
//                    mPasswordView.setError(getString(R.string.error_incorrect_password));
//                    mPasswordView.requestFocus();
//                    progress.dismiss();
//                }
//            });
        }
    }

    private boolean isPhoneValid(String Phone) {
        return Phone.length() == 11;
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 4;
    }

}

